layui.use(['table', 'admin', 'ax','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 停发地区设置管理
     */
    var ToolBasicStopFiring = {
        tableId: "toolBasicStopFiringTable"
    };

    /**
     * 初始化表格的列
     */
    ToolBasicStopFiring.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'states', sort: true, title: '状态', templet: '#statusTpl'},
            /*{field: 'expresscompanyName', sort: true, title: '快递公司'},*/
            {field: 'province', sort: true, title: '省份'},
            {field: 'city', sort: true, title: '城市'},
            {field: 'areas', sort: true, title: '区县'},
            {field: 'street', sort: true, title: '街道'},
            {field: 'address', sort: true, title: '地址'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ToolBasicStopFiring.search = function () {
        var queryData = {};
        queryData['states'] = $("#states").val();
        queryData['province'] = $("#province").val();
        queryData['city'] = $("#city").val();
        queryData['areas'] = $("#areas").val();
        queryData['street'] = $("#street").val();
        queryData['condition'] = $("#condition").val();
        table.reload(ToolBasicStopFiring.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    ToolBasicStopFiring.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/toolBasicStopFiring/add';
    };

    /**
     * 导出excel按钮
     */
    ToolBasicStopFiring.exportExcel = function () {
        var checkRows = table.checkStatus(ToolBasicStopFiring.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ToolBasicStopFiring.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/toolBasicStopFiring/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ToolBasicStopFiring.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/toolBasicStopFiring/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ToolBasicStopFiring.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ToolBasicStopFiring.tableId,
        url: Feng.ctxPath + '/toolBasicStopFiring/list',
        page: true,
        limit: 120,
		limits:[120,1000,2000,5000,20000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: ToolBasicStopFiring.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ToolBasicStopFiring.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ToolBasicStopFiring.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ToolBasicStopFiring.exportExcel();
    });

	 // 导入excel
	 $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '停发地区导入',
            area: ['1000px', '600px'],
            anim: 5,
            content: Feng.ctxPath + '/toolBasicStopFiring/toimportExcel',
            end: function () {
                table.reload(ToolBasicStopFiring.tableId);
            }
        });
	 });
	    
    $('#btnRevoke').click(function () {
    	var checkStatus = table.checkStatus('toolBasicStopFiringTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/toolBasicStopFiring/btnRevoke?ids="+ids, function (data) {
	                if(data.success){
	                	Feng.success("删除成功!");
	                	table.reload(ToolBasicStopFiring.tableId);
	                } else {
	                    Feng.error("删除失败!" + data.message + "!");
	                }
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除选中明细?", operation);
		} else {
			layer.alert('请至少选中一条数据');
		}
    });

    // 修改参数状态
    form.on('switch(states)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        ToolBasicStopFiring.changeGradingStatus(id, checked);
    });

    /**
     * 修改参数状态
     *
     * @param id 参数id
     * @param checked 是否选中（true,false），选中就是启用用户，未选中就是禁用用户
     */
    ToolBasicStopFiring.changeGradingStatus = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/toolBasicStopFiring/editItem2", function (data) {
                Feng.success("启用成功!");
            }, function (data) {
                Feng.error("禁用失败!" + data.responseJSON.message + "!");
                table.reload(ToolBasicStopFiring.tableId);
            });
            ajax.set("states", 1);
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/toolBasicStopFiring/editItem2", function (data) {
                Feng.success("禁用成功!");
            }, function (data) {
                Feng.error("禁用失败!" + data.responseJSON.message + "!");
                table.reload(ToolBasicStopFiring.tableId);
            });
            ajax.set("states", 2);
            ajax.set("id", id);
            ajax.start();
        }
    };

    // 工具条点击事件
    table.on('tool(' + ToolBasicStopFiring.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ToolBasicStopFiring.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ToolBasicStopFiring.onDeleteItem(data);
        }
    });
});
